package com.spark.rectify

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.spark.common.BaseActivity
import kotlinx.android.synthetic.main.act_complain.*

/**
 * 申诉
 */
class ComplainActivity : BaseActivity() {
    private lateinit var complainLemmaAdapter: ComplainLemmaAdapter

    companion object {
        fun actionStart(host: Activity) {
            val intent = Intent(host, ComplainActivity::class.java)

            host.startActivity(intent)
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_complain
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        iv_back.setOnClickListener { finish() }
        tv_title.text = "申诉"

        complainLemmaAdapter = ComplainLemmaAdapter(R.layout.item_complain)
        recy_lemma.adapter = complainLemmaAdapter
        complainLemmaAdapter.addData(mutableListOf(Lemma(), Lemma(), Lemma(), Lemma(), Lemma(), Lemma(), Lemma(), Lemma(), Lemma(), Lemma()))
    }
}

data class Lemma(
        var id: String = "",
        var name: String = "",
        var isChecked: Boolean = false
)

class ComplainLemmaAdapter(layoutRes: Int, var singleChoice: Boolean = false) : BaseQuickAdapter<Lemma, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: Lemma?) {
        if (item == null) return
        helper?.apply {
            getView<LinearLayout>(R.id.ll_item_container).apply {
                setBackgroundResource(
                        if (item.isChecked) R.drawable.bg_round10_rect_blue
                        else R.drawable.bg_round10_rect_white
                )
                setOnClickListener {
                    updateState(adapterPosition)
                }
            }

        }
    }

    private fun updateState(position: Int) {
        if (singleChoice) {
            data.forEachIndexed { index, lemma ->
                if (index == position) {
                    lemma.isChecked = !lemma.isChecked
                }
            }
        } else {
            val beforeState = data[position].isChecked
            data[position].isChecked = !beforeState
        }
    }

    fun summary(): MutableList<Lemma> {
        return data.filter {
            return@filter it.isChecked
        }.toMutableList()
    }
}